﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;

namespace Hisun.View
{
    public partial class lianjie : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0);
            }

        }

        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;
            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    var MyQuery = (from a in db.TABC_NC_AccCost where a.科目编码.Contains(TextBox_QueryValue.Text) select a).Skip(skipAccount).Take(10);
                    GridView_AccCost.DataSource = MyQuery;
                    GridView_AccCost.DataBind();
                    break;
                case 1:
                    var MyQuery2 = (from a in db.TABC_NC_AccCost where a.科目名称.Contains(TextBox_QueryValue.Text) select a).Skip(skipAccount).Take(10);
                    GridView_AccCost.DataSource = MyQuery2;
                    GridView_AccCost.DataBind();
                    break;
            }
            if (GridView_AccCost.Rows.Count == 0)
            { Panel_GridList.Visible = false; }
            else
            { Panel_GridList.Visible = true; }

        }


        //翻页的功能
        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            switch (DropDownList_Field.SelectedIndex)
            {
                case 0:
                    MyQuery = (from a in db.TABC_NC_AccCost where a.科目编码.Contains(TextBox_QueryValue.Text) select a).Count();
                    break;
                case 1:
                    MyQuery = (from a in db.TABC_NC_AccCost where a.科目名称.Contains(TextBox_QueryValue.Text) select a).Count();

                    break;
            }

            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

            if (GridView_AccCost.Rows.Count == 0)
            { Panel_GridList.Visible = false; }
            else
            { Panel_GridList.Visible = true; }

        }



        //选择产品列表，弹出对话框
        protected void Button_Select1_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('AccSelect.aspx');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('TextBox_SubjectCode').value=aa[0];");
            s.Append("document.all('TextBox_SubjectName').value=aa[1];}");
            //s.Append("document.all('TextBox_FuMa').value=aa[2];");
            //s.Append("document.all('TextBox_Unit').value=aa[3];}");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }

        }
        protected void Button_Select2_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('CostSelect.aspx');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('TextBox_CostCode').value=aa[0];");
            s.Append("document.all('TextBox_CostName').value=aa[1];}");
            //s.Append("document.all('TextBox_FuMa').value=aa[2];");
            //s.Append("document.all('TextBox_Unit').value=aa[3];}");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }

        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            Panel_AddEdit.Visible = true;
            Panel_GridList.Visible = false;
            StateFlag.Value = "edit";
            TextBox_SubjectCode.ReadOnly = true;



            string mystring = e.CommandArgument.ToString();
            var my = (from a in db.TABC_NC_AccCost where a.科目编码 == mystring select a).Single();
            TextBox_SubjectCode.Text = my.科目编码;

            TextBox_SubjectName.Text = my.科目名称;
            TextBox_CostCode.Text = my.成本细项编码;
            TextBox_CostName.Text = my.成本细项名称;
            //TextBox_Description.Text = my.车间;
        }

        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {

            StateFlag.Value = "add";
            TextBox_SubjectCode.ReadOnly = false;
            Panel_AddEdit.Visible = true;
            Panel_GridList.Visible = false;


            TextBox_SubjectCode.Text = "";
            TextBox_SubjectName.Text = "";
            TextBox_CostCode.Text = "";
            TextBox_CostName.Text = "";
            //TextBox_Description.Text = "";
            //TextBox_FuMa.Text = "";
            //TextBox_Unit.Text = "";
        }


        //del
        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            string myid = (string)e.CommandArgument;
            var my = (from a in db.TABC_NC_AccCost where a.科目编码 == myid select a).Single();

            db.TABC_NC_AccCost.DeleteOnSubmit(my);

            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除成功'); </script>");

                TextBox_SubjectCode.Text = "";
                TextBox_SubjectName.Text = "";
                TextBox_CostCode.Text = "";
                TextBox_CostName.Text = "";
                //TextBox_Description.Text = "";
                //TextBox_FuMa.Text = "";
                //TextBox_Unit.Text = "";

            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除失败'); </script>");

            }


        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {


            Panel_AddEdit.Visible = false;
            Panel_GridList.Visible = true;
            StateFlag.Value = "0";
            if (StateFlag.Value == "edit")
            {
                TextBox_SubjectCode.ReadOnly = false;

            }

        }

        protected void Button_PostBack_Command(object sender, CommandEventArgs e)
        {
            if (StateFlag.Value == "edit")
            {
                var my = (from a in db.TABC_NC_AccCost where a.科目编码 == TextBox_SubjectCode.Text.Trim() select a).Single();

                TextBox_SubjectCode.ReadOnly = true;

                //my.科目编码 = TextBox_SubjectCode.Text;
                my.科目名称 = TextBox_SubjectName.Text;
                my.成本细项编码 = TextBox_CostCode.Text;
                my.成本细项名称 = TextBox_CostName.Text;


                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('科目对照修改成功'); </script>");

                    TextBox_SubjectCode.ReadOnly = true;
                    Panel_AddEdit.Visible = false;
                    Panel_GridList.Visible = true;

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改失败'); </script>");

                }




            }
            else if (StateFlag.Value == "add")
            {
                var my = from a in db.TABC_NC_AccCost where a.科目编码 == TextBox_SubjectCode.Text.Trim() select a;
                if (my.Count() > 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('此会计科目已存在'); </script>");
                    return;

                }


                TextBox_SubjectCode.ReadOnly = true;

                TABC_NC_AccCost MyAccCost = new TABC_NC_AccCost();

                MyAccCost.科目编码 = TextBox_SubjectCode.Text.Trim();
                MyAccCost.科目名称 = TextBox_SubjectName.Text;
                MyAccCost.成本细项编码 = TextBox_CostCode.Text;
                MyAccCost.成本细项名称 = TextBox_CostName.Text;

                db.TABC_NC_AccCost.InsertOnSubmit(MyAccCost);
                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('科目对照建立成功'); </script>");


                    TextBox_SubjectCode.ReadOnly = false;
                    Panel_AddEdit.Visible = false;
                    Panel_GridList.Visible = true;

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('建立失败'); </script>");

                }




            }
        }
      }
     
}
